package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.dto.AddCourseDto;
import com.xuecheng.content.model.dto.AddCourseTeacherDto;
import com.xuecheng.content.model.dto.CourseTeacherDto;
import com.xuecheng.content.model.dto.EditCourseTeacherDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import com.xuecheng.exception.XueChengPlusException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CourseTeacherImpl implements CourseTeacherService {
    @Autowired
    private CourseTeacherMapper courseTeacherMapper;
    @Autowired
    private CourseBaseMapper courseBaseMapper;

    //查询教师计划
    @Override
    public List<CourseTeacherDto> courseTeacherList(Long courseId) {
        List<CourseTeacher> courseTeachers =
                courseTeacherMapper.selectList(new LambdaQueryWrapper<CourseTeacher>().eq(CourseTeacher::getCourseId, courseId));
        List<CourseTeacherDto> list=new ArrayList<>();
        for(CourseTeacher item:courseTeachers){
            CourseTeacherDto courseTeacherDto = new CourseTeacherDto();
            BeanUtils.copyProperties(item,courseTeacherDto);
            list.add(courseTeacherDto);
        }
        return list;
    }

    //新增教师信息/同时修改教师信息
    @Override
    public CourseTeacherDto addAndModifyCourseTeacher(Long compangId, CourseTeacherDto courseDto) {
        CourseTeacher courseTeacher = courseTeacherMapper.selectById(courseDto.getId());
        if(courseTeacher==null){
            CourseTeacher courseTeacherNew = new CourseTeacher();
            BeanUtils.copyProperties(courseDto,courseTeacherNew);
            courseTeacherNew.setCreateDate(LocalDateTime.now());
            int insert=courseTeacherMapper.insert(courseTeacherNew);
            if(insert<=0){
                throw new XueChengPlusException("新增教师信息失败");
            }
            return getCourseTeacherDto(courseTeacherNew.getId());
        }else {
            CourseBase courseBase = courseBaseMapper.selectById(courseTeacher.getCourseId());
            if(!courseBase.getCompanyId().equals(compangId)){
                throw new XueChengPlusException("只允许向机构自己的课程中添加老师、删除老师");
            }
            BeanUtils.copyProperties(courseDto,courseTeacher);
            courseTeacherMapper.updateById(courseTeacher);
            return getCourseTeacherDto(courseTeacher.getId());
        }
    }

    //修改教师信息
    @Override
    public CourseTeacherDto modifyCourseTeacher(Long compangId, CourseTeacher dto) {
        CourseTeacher courseTeacher = courseTeacherMapper.selectById(dto.getId());
        if(courseTeacher==null){
            throw new XueChengPlusException("教师不存在");
        }
        CourseBase courseBase = courseBaseMapper.selectById(courseTeacher.getCourseId());
        if(!courseBase.getCompanyId().equals(compangId)){
            throw new XueChengPlusException("只允许向机构自己的课程中添加老师、删除老师");
        }
        BeanUtils.copyProperties(dto,courseTeacher);
        courseTeacherMapper.updateById(courseTeacher);

        return getCourseTeacherDto(courseTeacher.getId());
    }

    @Override
    public void deleteCourseTeacher(Long compangId, Long courseId, Long teacherId) {
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        if(!courseBase.getCompanyId().equals(compangId)){
            throw new XueChengPlusException("只允许向机构自己的课程中添加老师、删除老师");
        }
        courseTeacherMapper.deleteById(teacherId);
    }

    private CourseTeacherDto getCourseTeacherDto(Long teacherId) {
        CourseTeacher courseTeacher = courseTeacherMapper.selectById(teacherId);
        CourseTeacherDto courseTeacherDto = new CourseTeacherDto();
        BeanUtils.copyProperties(courseTeacher,courseTeacherDto);
        return courseTeacherDto;
    }

}
